<template>
  <div>
    <el-dialog v-bind="$attrs" :visible.sync="showDialog" v-on="$listeners" @open="onOpen" @close="onClose" title="">
      <el-row :gutter="15">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="父级科室" prop="parentId">
              <el-select v-model="formData.parentId" placeholder="请选择父级科室" clearable :style="{width: '100%'}">
                <el-option v-for="(item, index) in parentIdOptions" :key="index" :label="item.label"
                  :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="科室名称" prop="departmentName">
              <el-input v-model="formData.departmentName" placeholder="请输入科室名称" clearable
                :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="科室主任" prop="departmentDirector">
              <el-input v-model="formData.departmentDirector" placeholder="请输入科室主任" clearable
                :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="科室描述" prop="departmentDescription">
              <el-input v-model="formData.departmentDescription" type="textarea" placeholder="请输入科室描述"
                :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {saveDepartment} from '../../api/dept'
export default {

  inheritAttrs: false,
  components: {},
  props: {
    addOrUpdateVisible:{
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      showDialog:false,
      formData: {
        parentId: undefined,
        departmentName: undefined,
        departmentDirector: undefined,
        departmentDescription: undefined,
      },
      rules: {
        parentId: [{
          required: true,
          message: '请选择父级科室',
          trigger: 'change'
        }],
        departmentName: [{
          required: true,
          message: '请输入科室名称',
          trigger: 'blur'
        }],
        departmentDirector: [{
          required: true,
          message: '请输入科室主任',
          trigger: 'blur'
        }],
        departmentDescription: [{
          required: true,
          message: '请输入科室描述',
          trigger: 'blur'
        }],
      },
      parentIdOptions: [{
        "label": "内科",
        "value": 1
      }, {
        "label": "外科",
        "value": 2
      }, {
        "label": "妇产科",
        "value": 15
      }, {
        "label": "儿科",
        "value": 18
      }, {
        "label": "耳鼻喉科",
        "value": 21
      }, {
        "label": "肿瘤科",
        "value": 24
      }, {
        "label": "口腔科",
        "value": 26
      }, {
        "label": "病理科",
        "value": 28
      }, {
        "label": "神经科",
        "value": 30
      }, {
        "label": "非临床科室",
        "value": 32
      }],
    }
  },
  computed: {},
  watch: {
     addOrUpdateVisible(oldVal,newVal){
      this.showDialog = this.addOrUpdateVisible
    }
  },
  created() {},
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['elForm'].resetFields()
      this.$emit('changeShow','false')
    },
    close() {
      this.$emit('update:visible', false)
      this.$emit('changeShow','false')
    },
    async addDepartment() {
      const { data:res } = await saveDepartment(this.formData)
      if (res.code == 200) {
        this.$notify.success({
          title: '操作成功',
          message: '科室添加成功',
        })
        //关闭
        this.$emit('changeShow','false')
        this.formData = {}
        this.$emit('getDepartment');
      //  this.$emit('getDeptAndCount');
      } else {
        return this.$message.error('科室添加失败：'+res.message)
      }
    },
    handelConfirm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) {return}
        console.log(this.formData)
        this.addDepartment()
      })
    },
  }
}

</script>
<style>
</style>
